﻿CREATE PROCEDURE [ServiceEngine].[UpdateOperationVersion]
	@VersionMajor int,
	@VersionMinor int,
	@VersionBuild int,
	@VersionQFE int,
	@Description ntext = null,
	@AutoResponse Xml,
	@IsPublic bit,
	@IsActive bit,
	@IsTestable bit,
	@RequestTransform xml = null,
	@ResponseTransform xml = null,
	@ResourceID uniqueidentifier = null,	
	@PolicyID uniqueidentifier = null,
	@ID uniqueidentifier 
AS
BEGIN
	DECLARE @ErrorMessage NVARCHAR(4000);
	DECLARE @ErrorSeverity INT;
	DECLARE @ErrorState INT;
	SET NOCOUNT ON;

	BEGIN TRY
		BEGIN TRANSACTION

		UPDATE	ServiceEngine.OperationVersions 
		SET		VersionMajor = @VersionMajor,
				VersionMinor = @VersionMinor, 
				VersionBuild = @VersionBuild, 
				VersionQFE = @VersionQFE,
				Description = @Description,
				AutoResponse = @AutoResponse, 
				IsPublic = @IsPublic, 
				IsActive = @IsActive,
				IsTestable = @IsTestable,
				RequestTransformXSLT = @RequestTransform,
				ResponseTransformXSLT = @ResponseTransform,
				ResourceID = @ResourceID,
				PolicyID = @PolicyID
		WHERE	OperationVersionID = @ID

		--Delete any Public associations with Endpoint
		  IF @IsPublic = 0 
		  BEGIN
			DELETE ServiceEngine.Endpoints_PublishedOperations 
			WHERE OperationVersionID = @ID
		  END

		  --Delete any Active associations with Endpoint
		  IF @IsActive = 0 AND @IsTestable = 0
		  BEGIN
			  DELETE ServiceEngine.Endpoints_AssociatedOperations 
			  WHERE OperationVersionID = @ID
		  END

		COMMIT TRANSACTION
	END TRY
	BEGIN CATCH
		SELECT 
			@ErrorMessage = ERROR_MESSAGE(),
			@ErrorSeverity = ERROR_SEVERITY(),
			@ErrorState = ERROR_STATE();

		IF XACT_STATE() <> 0 ROLLBACK TRANSACTION;

		RAISERROR (@ErrorMessage, 
				   @ErrorSeverity,
				   1);
	END CATCH
END


